<template>
  <div class="login-container">
    <el-card class="login-card">
      <template #header>
        <div class="card-header">
          <span>虚拟号码租赁系统</span>
        </div>
      </template>
      <el-form
        :model="loginForm"
        :rules="loginRules"
        ref="loginFormRef"
        label-width="100px"
        class="login-form"
      >
        <el-form-item label="账号" prop="account">
          <el-input v-model="loginForm.account" placeholder="请输入账号"></el-input>
        </el-form-item>

        <div v-if="loginType === 'password'">
          <el-form-item label="密码" prop="password">
            <el-input v-model="loginForm.password" type="password" placeholder="请输入密码"></el-input>
          </el-form-item>
        </div>

        <el-form-item>
          <el-button type="primary" @click="submitLogin" :loading="isLoggingIn">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <div class="login-footer">
      <p>© 2025 虚拟手机号码租赁系统 - 安全、便捷、高效</p>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { loginAPI, getUserInfoAPI } from '@/apis/sysAPI'
import { useUserStore } from '@/stores/sysuser'

// 获取路由实例和用户Store
const router = useRouter()
const userStore = useUserStore()

// 表单引用
const loginFormRef = ref()

// 登录方式：password
const loginType = ref('password')

// 表单数据
const loginForm = reactive({
  account: '',
  password: ''
})

// 表单验证规则
const loginRules = {
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 20, message: '账号长度在3-20个字符之间', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少6位', trigger: 'blur' }
  ]
}

// 登录状态
const isLoggingIn = ref(false)

// 提交登录
const submitLogin = async () => {
  if (!loginFormRef.value) return

  try {
    // 表单验证
    await loginFormRef.value.validate()
    isLoggingIn.value = true

    // 调用登录API
    const loginResponse = await loginAPI({
      account: loginForm.account,
      password: loginForm.password
    })

    // 处理登录响应
    if (loginResponse.code === 0) {
      const loginInfo = loginResponse.data

      // 验证用户ID是否存在
      if (!loginInfo.userId) {
        ElMessage.error('登录信息异常未获取到用户ID')
        return
      }

      // 获取用户详细信息
      const userInfoResponse = await getUserInfoAPI(loginInfo.sysUserId)
      console.log("userInfoResponse", userInfoResponse.data);


      if (userInfoResponse.code === 0) {
      console.log("成功获取用户信息");

      userStore.setUserInfo(userInfoResponse.data)
      ElMessage.success('登录成功')
      router.push('/main')
      } else {
        ElMessage.error(userInfoResponse.msg || '获取用户信息失败')
      }
    } else {
      ElMessage.error(loginResponse.msg || '登录失败，请重试')
    }
  } catch (error) {
    // 处理验证失败和API错误
    console.error('登录错误详情:', error)
    if (error.errors && error.errors.length > 0) {
      ElMessage.error(error.errors[0].message || '表单验证失败')
    } else if (error.message) {
      ElMessage.error(error.message)
    } else {
      ElMessage.error('登录失败，请检查网络或联系管理员')
    }
  } finally {
    isLoggingIn.value = false
  }
}

onMounted(() => {})
</script>

<style scoped lang="scss">
.login-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;

  .login-card {
    width: 100%;
    max-width: 500px;
    border-radius: 10px;
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);

    .card-header {
      text-align: center;
      font-size: 28px;
      font-weight: bold;
      color: #333;
    }

    .login-form {
      margin-top: 20px;

      .el-form-item {
        margin-bottom: 20px;
      }
    }
  }

  .login-footer {
    margin-top: 30px;
    text-align: center;
    color: white;
    font-size: 14px;
  }
}
</style>
